package com.fita.webpay.entity.callback;

import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.demo2do.core.wechat.entity.pay.PayConfig;
import com.demo2do.core.wechat.entity.pay.PayTransaction;
import com.fita.webpay.entity.PayChannel;
import com.fita.webpay.entity.config.HandpayConfig;

public class HandpayCallbackRequest implements CallbackRequest {

    private PayConfig payConfig;
    
    private PayTransaction payTransaction;
    
    private Map<String, String> requestParameters;
    
    private String transStatus;
    
    private String actualAmount;
    
    private String statusCode;
    
    private String statusMsg;

    /**
     * @param requestParameters
     */
    public HandpayCallbackRequest(Map<String, String> requestParameters) {
        
        // init request_parameters
        this.requestParameters = requestParameters;
        
        // init pay_config
        this.payConfig = initPayConfig();
        
        // init pay_transaction
        this.payTransaction = initPayTransaction();
        
        this.transStatus = this.requestParameters.get("transStatus");
        
        this.actualAmount = this.requestParameters.get("actualAmount");
        
        this.statusCode = this.requestParameters.get("statusCode");
        
        this.statusMsg = this.requestParameters.get("statusMsg");
        
    }
    /**
     * 
     * @return
     */
    private PayConfig initPayConfig() {
        
        // get params from request_parameters
        String sellerId = this.requestParameters.get("hpMerCode");
        
        // construct handpay config
        return new HandpayConfig(sellerId);
    }
    
    /**
     * 
     * @return
     */
    private PayTransaction initPayTransaction() {
        
        String outTradeNo = requestParameters.get("orderNo");
        String transactionCode = requestParameters.get("transSeq");
        String amount = requestParameters.get("transAmount");
        String paymentTime = requestParameters.get("transDate");
        
        return new CallbackContext(outTradeNo, transactionCode, amount, paymentTime);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#generateSignString()
     */
    public String generateSignString() {
        return this.payConfig.getSellerId() + "|" + this.payTransaction.getOutTradeNo() + "|" +
               this.payTransaction.getPaymentTimeAsString() + "|" + this.transStatus + "|" +
               this.payTransaction.getAmount() + "|" + this.actualAmount + "|" + 
               this.payTransaction.getTransactionCode() + "|" + this.statusCode + "|" +
               this.statusMsg;
    }
    
    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#getRequestParameters()
     */
    public Map<String, String> getRequestParameters() {
        return requestParameters;
    }
    
    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#getPayChannel()
     */
    public PayChannel getPayChannel() {
        return PayChannel.HANDPAY;
    }

    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#getPayConfig()
     */
    public PayConfig getPayConfig() {
        return this.payConfig;
    }
    
    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#getPayTransaction()
     */
    public PayTransaction getPayTransaction() {
        return this.payTransaction;
    }
    
    public String getStatusCode() {
        return statusCode;
    }
    
    public String getStatusMsg() {
        return statusMsg;
    }

    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#isValid()
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(this.payTransaction.getOutTradeNo());
    }
    
    /* (non-Javadoc)
     * @see com.fita.webpay.entity.callback.CallbackRequest#getSign()
     */
    public String getSign() {
        return requestParameters.get("signature");
    }
    /**
     * @return the transStatus
     */
    public String getTransStatus() {
        return transStatus;
    }
    /**
     * @return the actualAmount
     */
    public String getActualAmount() {
        return actualAmount;
    }
    /**
     * @param payConfig the payConfig to set
     */
    public void setPayConfig(PayConfig payConfig) {
        this.payConfig = payConfig;
    }
    /**
     * @param payTransaction the payTransaction to set
     */
    public void setPayTransaction(PayTransaction payTransaction) {
        this.payTransaction = payTransaction;
    }
    /**
     * @param requestParameters the requestParameters to set
     */
    public void setRequestParameters(Map<String, String> requestParameters) {
        this.requestParameters = requestParameters;
    }
    /**
     * @param transStatus the transStatus to set
     */
    public void setTransStatus(String transStatus) {
        this.transStatus = transStatus;
    }
    /**
     * @param actualAmount the actualAmount to set
     */
    public void setActualAmount(String actualAmount) {
        this.actualAmount = actualAmount;
    }
    /**
     * @param statusCode the statusCode to set
     */
    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }
    /**
     * @param statusMsg the statusMsg to set
     */
    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }
}
